package com.cmict.data.form.api.task;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.cmict.data.form.api.entity.SysTableColumn;
import com.cmict.data.form.api.entity.vo.SysFromDataColumnVo;
import com.cmict.data.form.api.entity.vo.SysFromUpDataVo;
import com.cmict.data.form.api.service.SysCurrencyService;
import com.cmict.data.form.api.task.dto.BpmColumnDTO;
import com.cmict.data.form.api.task.dto.FormBaseDTO;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.util.collection.CollectionUtils;
import com.cmict.data.form.api.convert.BpmFormConvert;
import com.cmict.data.form.api.convert.SysFormConvert;
import com.cmict.data.form.api.task.dto.BpmFormDTO;
import com.cmict.data.form.api.task.dto.SysFormDTO;
import com.cmict.data.form.api.entity.BpmForm;
import com.cmict.data.form.api.entity.dataobject.SysFormDO;
import com.cmict.data.form.api.mapper.SysEditionMapper;
import com.cmict.data.form.api.service.SysFormService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 表单数据 API 实现类
 *
 * @author awesome
 */
@RestController
public class FormDataApiImpl implements FormDataApi {

    @Resource
    private SysFormService sysFormService;

    @Resource
    private SysEditionMapper sysEditionMapper;
    @Resource
    private SysCurrencyService sysCurrencyService;

    @Override
    public Map<Long, FormBaseDTO> getFormMap(Set<Long> formIds) {
        if (CollUtil.isEmpty(formIds)) {
            return Collections.emptyMap();
        }
        return CollectionUtils.convertMap(SysFormConvert.INSTANCE.convertList01(sysFormService.getFormList(formIds)), FormBaseDTO::getId);
    }

    @Override
    public BpmFormDTO getFormConfig(Long formId) {
        BpmForm bpmForm = new BpmForm();
        SysFormDO sysFormDO = sysFormService.selectById(formId);
        bpmForm.setId(sysFormDO.getId());
        bpmForm.setName(sysFormDO.getFormName());
        JSONObject conf = JSONObject.parseObject(sysFormDO.getFormJsonUser());
        String fields= conf.getJSONArray("fields").toString();
        //再将String转为List<String>
        List<String> list = JSONObject.parseArray(fields,String.class);
        bpmForm.setFields(list);

        bpmForm.setConf(conf.fluentRemove("fields").toJSONString());
        return BpmFormConvert.INSTANCE.convert(bpmForm);
    }

    @Override
    public Long getFormIdByTableName(String tableName) {
//        SysFormDO sysFormDO = sysFormService.selectOne(tableName);
        SysFormDO sysFormDO = sysFormService.selectOneByTableName(tableName);
        return sysFormDO != null ? sysFormDO.getId() : 0L;
    }
    @Override
    public Set<Long> getUserMenuId() {
        return sysFormService.getUserMenuId();
    }

    @Override
    public List<SysFormDTO> getSysFormByUseMenuIds(String menuIds) {
        return SysFormConvert.INSTANCE.convertList(sysFormService.getSysFormByUseMenuIds(menuIds));
    }

    @Override
    public List<SysFormDTO> selectListRelease() {
        return SysFormConvert.INSTANCE.convertList(sysFormService.selectListRelease());
    }

    @Override
    public Collection<? extends Long> getManageMenuId() {
        return sysFormService.getManageMenuId();
    }

    @Override
    public Set<SysFormDTO> getSysFormByFormMenuIds(String formMenuId) {
        return SysFormConvert.INSTANCE.convertSet(sysFormService.getSysFormByFormMenuIds(formMenuId));
    }


    @Override
    public CommonResult selectAllData(String tableName) {
        String sql = "SELECT * FROM "+ tableName + " ORDER BY create_time DESC,id DESC;";
        //ArrayList data = (ArrayList) sysCurrencyMapper.selectData(sql);
        //远端数据映射
        //List<Map<Object,Object>> dataMapList = sysCurrencyMapper.selectData(sql);
        //ArrayList data= (ArrayList) AssemblyTypeUtil.associatedDataMapping(tableName,dataMapList).getData();
        return  CommonResult.success("获取成功");
    }

    @Override
    public CommonResult getDataByIds(Integer formRecordId,Long formId) {
        SysFormDO form = sysFormService.getSysForm(formId);
        if(null != form && StringUtils.isNotEmpty(form.getTableName())){
            String sql = "SELECT * FROM "+ form.getTableName() + " where id = " + formRecordId +" ;";
            Map<Object,Object> data = sysCurrencyService.getDataOne(sql);
            return CommonResult.success("获取成功", data );
        }
        return CommonResult.success("获取失败", null );
    }

    @Override
    public CommonResult getDataBydataIds(Integer dataId, Long formId) {
        SysFormDO form = sysFormService.getSysForm(formId);
        if(null != form && StringUtils.isNotEmpty(form.getTableName())){
            String sql = "SELECT count(0) as total FROM "+ form.getTableName() + " where data_id = " + dataId +" ;";
            Map<Object,Object> data = sysCurrencyService.getDataOne(sql);
            return CommonResult.success("获取成功", data );
        }
        return CommonResult.success("获取失败", null );
    }

    public SysFormDTO getSysFormById(Long forId){
        SysFormDO sysForm = sysFormService.getSysForm(forId);
        return SysFormConvert.INSTANCE.convert(sysForm);
    }

    @Override
    public CommonResult<List<BpmColumnDTO>> getColumnByFormId(Long formId) {
        SysFormDO form = sysFormService.getSysForm(formId);
        if(ObjectUtil.isNotEmpty(form) && ObjectUtil.isNotEmpty(form.getTableName())){
            List<SysTableColumn> list = sysCurrencyService.getColumn(form.getTableName());
            List<BpmColumnDTO> columnList = list.parallelStream().map(item -> {
                BpmColumnDTO bpmColumnDTO = new BpmColumnDTO();
                item.setColumn(item.getColumnName() + "_" + item.getColumnComment());
                BeanUtils.copyProperties(item, bpmColumnDTO);
                return bpmColumnDTO;
            }).collect(Collectors.toList());
            return CommonResult.success("获取成功",columnList);
        }else{
            return CommonResult.error(500,"表单已不存在");
        }
    }

    @Override
    public CommonResult updata(String tableName, Integer formRecordId,Integer approveStatus) {
        SysFromUpDataVo sysFromUpData = new SysFromUpDataVo();
        sysFromUpData.setTableName(tableName);
        sysFromUpData.setDataId(String.valueOf(formRecordId));
        List<SysFromDataColumnVo> data = new ArrayList<>();
        SysFromDataColumnVo sysFromDataColumn  = new SysFromDataColumnVo();
        sysFromDataColumn.setName("approve_status");
        sysFromDataColumn.setValue(String.valueOf(approveStatus));
        sysFromDataColumn.setType("TINYINT");
        data.add(sysFromDataColumn);
        sysFromUpData.setData(data);
        List<String> list = sysCurrencyService.getColumnName(tableName);
        if(list.contains("approve_status")){
            return sysCurrencyService.upData(sysFromUpData);
        }else{
            return null;
        }
    }
}
